#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e7+4;
char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)) x=x*10+(ch^48),ch=getchar();
    return x*f;
}
int n,mod,inv2;
#define pow Pow
int pow(int x,int base){
    if(base<=0) return 1;
    int ans=1;
    while(base){
        if(base&1) ans=ans*x%mod;
        x=x*x%mod;
        base>>=1;
    }
    return ans;
}
int ans,jc[N],ijc[N];
inline int C(int n,int m){
    return jc[m]*ijc[n]%mod*ijc[m-n]%mod;
}
inline int calc(int n){
    if(n==1) return 1;
    return jc[n]*inv2%mod;
}
signed main(){
    freopen("tree.in","r",stdin);
    freopen("tree.out","w",stdout);
    n=read(),mod=read();
    inv2=(mod+1)/2;
    if(n==1){
        cout<<1;
        return 0;
    }
    jc[0]=1;
    for(int i=1;i<=n;i++) jc[i]=jc[i-1]*i%mod;
    ijc[n]=pow(jc[n],mod-2);
    for(int i=n-1;~i;i--) ijc[i]=ijc[i+1]*(i+1)%mod;
    int pp=2,p2=1;
    for(int i=n-1;i;i--){
        int res=C(i,n)*i%mod*calc(i)%mod*pp%mod;
        ans=(ans+((i!=1)+1)*p2%mod*res%mod)%mod;
        pp=pp*n%mod;
        p2=(p2<<1)%mod;
    }
    ans=(ans+calc(n)*2)%mod;
    cout<<ans;
    return 0;
}